home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / utils / console / splitvt-.000 / splitvt- / splitvt-1.6.1 / config.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-13  |  7.0 KB  |  278 lines

  1. #include    <sys/types.h>
  2. #include    <stdio.h>
  3. #include    <sys/stat.h>
  4. #include    <signal.h>
  5.  
  6. /*#define DEBUG        /* Provides extra debugging info */
  7.  
  8. #define VERBOSE_PRINT(X)    if ( verbose ) printf(X)
  9.  
  10. #define F_OK    0
  11. #define X_OK    1
  12. #define W_OK    2
  13. #define R_OK    4
  14.  
  15. int exists(dir, file)
  16. char *dir;
  17. char *file;
  18. {
  19.     struct stat statbuf;
  20.     char buffer[BUFSIZ];
  21.  
  22.     sprintf(buffer, "%s/%s", dir, file);
  23.     if ( stat(buffer, &statbuf) == 0 )
  24.         return(1);
  25.     else
  26.         return(0);
  27. }
  28.  
  29.  
  30. main(argc, argv)
  31. int argc;
  32. char *argv[];
  33. {
  34.     int  irix=0, solaris=0, verbose=0, write_utmp;
  35.     char cflags[BUFSIZ], ldflags[BUFSIZ];
  36.     char line[BUFSIZ];
  37.     FILE *makefile;
  38.  
  39.     if ( argv[1] && strncmp(argv[1], "-v", 2) == 0 )
  40.         verbose=1;
  41.  
  42.     if ( exists(".", "Makefile") )
  43.     {
  44.         fprintf(stderr, "Makefile already exists.  Continue?  [y/n] ");
  45.         fgets(line, BUFSIZ-1, stdin);
  46.         if ( line[0] == 'y' )
  47.             printf("Continuing...\n");
  48.         else
  49.         {
  50.             printf("Configuration aborted. Exiting.\n");
  51.             exit(0);
  52.         }
  53.     }
  54.  
  55.     if ( (makefile=fopen("Makefile", "w")) == NULL )
  56.     {
  57.         perror("Can't create Makefile");
  58.         printf("Configuration aborted. Exiting.\n");
  59.         exit(2);
  60.     }
  61.  
  62. #ifdef DEBUG
  63.     strcpy(cflags, "-g -DDEBUG");
  64. #else
  65.     cflags[0]='\0';
  66. #endif
  67.     ldflags[0]='\0';
  68.  
  69. #define INCLUDE "/usr/include/"
  70. #ifdef linux
  71.     printf("Aha!  You are running Linux!\n");
  72.     printf("\tCongratulations on the choice of a GNU generation.\n");
  73.  
  74.     strcat(cflags, " -O2");
  75. #else
  76.  
  77. #ifdef NeXT
  78.     printf("NeXTStep!  I'm assuming 3.0 or greater...\n");
  79.     strcat(cflags, " -O2");
  80. #undef INCLUDE
  81. #define INCLUDE "/usr/include/bsd/"
  82. #else
  83.     /* Check for HP-UX */
  84.     if ( exists("/", "hp-ux") ) {
  85.         VERBOSE_PRINT("\tI see you are running HP-UX.\n");
  86.         strcat(cflags, " -DHP_UX");
  87.     } else if ( exists("/usr/lib", "libsocket.a") ) {   /* Solaris 2.1 */
  88.         strcat(ldflags, " -lsocket");
  89.         strcat(cflags, " -g -DSOLARIS");
  90.         solaris=1;
  91.         VERBOSE_PRINT("\tI see you are running Solaris.\n");
  92.     }
  93.     else
  94.         strcat(cflags, " -O");
  95.  
  96.     /* Check for IRIX */
  97.     if ( grep("/usr/include", "unistd.h", "_getpty") )
  98.     {
  99.         strcat(cflags, " -DIRIX");
  100.         irix=1;
  101.         VERBOSE_PRINT("\tI see you are running IRIX.\n");
  102.     }
  103. #endif /* NeXT */
  104. #endif /* linux */
  105.  
  106.     /* Check for the termcap library */
  107.     if ( exists("/usr/lib", "libtermcap.a") ) {
  108.         strcat(cflags, " -DTERMCAP");
  109.         strcat(ldflags, " -ltermcap");
  110.         VERBOSE_PRINT("\tUsing the termcap library for terminal support.\n");
  111.     }
  112.  
  113.     /* Check for the ut_host field in the utmp file */
  114.     if ( grep(INCLUDE, "utmp.h", "ut_host") ) {
  115.         strcat(cflags, " -DHAVE_UTHOST");
  116.         VERBOSE_PRINT("\tYour utmp file uses the host field.\n");
  117.     }
  118.  
  119.     /* Check for termio.h */
  120.     if ( exists(INCLUDE, "termio.h") ) {
  121.         strcat(cflags, " -DHAVE_TERMIO_H");
  122.         VERBOSE_PRINT("\tI will use termio tty structures.\n");
  123.     } else 
  124.         VERBOSE_PRINT("\tI will use sgttyb tty structures.\n");
  125.  
  126.     /* Check for unistd.h */
  127.     if ( exists(INCLUDE, "unistd.h") )
  128.         strcat(cflags, " -DHAVE_UNISTD_H");
  129.  
  130.     /* Check for BSD tty compatibility. (HP-UX) */
  131.     /* Check for BSD tty compatibility. (HP-UX) */
  132.     if ( exists(INCLUDE, "sys/bsdtty.h") ) {
  133.         strcat(cflags, " -DHAVE_BSDTTY_H");
  134.         VERBOSE_PRINT("\tI see you have BSD tty support.\n");
  135.     }
  136.  
  137.     /* Check for ioctl compatibility.  (FreeBSD) */
  138.     if ( exists(INCLUDE, "sys/ioctl_compat.h") ) {
  139.         strcat(cflags, " -DNEED_COMPAT_H");
  140.         VERBOSE_PRINT(
  141.             "\tI will use your ioctl compatibility header.\n");
  142.     }
  143.  
  144.     /* Check for BSD socket library header (AT&T) */
  145.     if ( exists(INCLUDE, "sys/inet.h") ) {
  146.         strcat(cflags, " -DNEED_INET_H");
  147.         VERBOSE_PRINT("\tI will use your inet compatibility header.\n");
  148.     }
  149.  
  150.     /* Check for BSD socket library header (AIX) */
  151.     if ( exists(INCLUDE, "sys/select.h") ) {
  152.         strcat(cflags, " -DNEED_SELECT_H");
  153.         VERBOSE_PRINT("\tI will use your sockets definition header.\n");
  154.     }
  155.  
  156.     /* Check for wait4() (This always seems to be true.) */
  157.     if ( grep(INCLUDE, "sys/wait.h", "wait4") ) {
  158.         strcat(cflags, " -DHAVE_WAIT4");
  159.         VERBOSE_PRINT("\tI will use wait4() instead of waitpid().\n");
  160.     }
  161.  
  162.     if ( exists("/usr/lib", "libnet.a") )    /* Socket routines */
  163.         strcat(ldflags, " -lnet");
  164.     if ( exists("/usr/lib", "libnsl.a") )    /* AT&T socket library */
  165.         strcat(ldflags, " -lnsl");
  166.     if ( exists("/usr/lib", "libnsl_s.a") )    /* AT&T socket library */
  167.         strcat(ldflags, " -lnsl_s");
  168.     if ( exists("/usr/lib", "libsun.a") )    /* IRIX yp routines */
  169.         strcat(ldflags, " -lsun");
  170.  
  171.     /* Tell the user what kind of configuration to do */
  172.     if ( (access("/etc/utmp", (R_OK|W_OK)) == 0) && getuid() )
  173.         write_utmp=1;
  174.     else
  175.         write_utmp=0;
  176.  
  177.     VERBOSE_PRINT("\n");
  178.     if ( solaris || irix ) {
  179.         if ( write_utmp ) {
  180.             VERBOSE_PRINT(
  181.     "This program doesn't need to be installed set-uid root.\n");
  182.             VERBOSE_PRINT(
  183.     "\nThis program will put entries for its windows in /etc/utmp.\n");
  184.         } else {
  185.             VERBOSE_PRINT(
  186.     "If installed set-uid root, this program will put entries for its windows\nin /etc/utmp.\n");
  187.         }
  188.     } else if ( write_utmp ) {
  189.         VERBOSE_PRINT(
  190.     "This program will put entries for its windows in /etc/utmp.\n");
  191.         VERBOSE_PRINT(
  192.     "\nIf installed set-uid root, this program will change ownership of the\n");
  193.         VERBOSE_PRINT(
  194.     "ttys acquired to the user running this program.\n");
  195.     } else {
  196.         VERBOSE_PRINT(
  197.     "If installed set-uid root, this program will put entries for its windows\n");
  198.         VERBOSE_PRINT(
  199.     "in /etc/utmp, and will also change ownership of the ttys it acquires to the\n");
  200.         VERBOSE_PRINT(
  201.     "user running this program.\n");
  202.     }
  203.     VERBOSE_PRINT("\n");
  204.  
  205.  
  206.     if ( exists("/bin", "csh") )
  207.         strcat(cflags, " -DSHELL=\\\"/bin/csh\\\"");
  208.     else
  209.         strcat(cflags, " -DSHELL=\\\"/bin/sh\\\"");
  210.  
  211.     fprintf(makefile, 
  212. "# This Makefile has been generated from the Configure script.\n");
  213.     fprintf(makefile, 
  214. "# Shareware copyright 1993, by Sam Lantinga\n\n");
  215. #ifdef linux
  216.     fprintf(makefile, "\nCC = gcc\n");
  217. #endif
  218.     fclose(makefile);
  219.     system("sh scanpty >>Makefile");
  220.     if ( (makefile=fopen("Makefile", "a")) == NULL )
  221.     {
  222.         perror("Can't finish creating Makefile");
  223.         printf("Configuration aborted. Exiting.\n");
  224.         exit(2);
  225.     }
  226.     fprintf(makefile, 
  227.         "PTYOPTS = -DPTYCHAR=$(PTYCHAR) -DHEXDIGIT=$(HEXDIGIT)\n");
  228.     sprintf(line, "\nCFLAGS = %s $(PTYOPTS)\nLIBS = %s\n", cflags, ldflags);
  229.     fprintf(makefile, "%s", line);
  230.     fprintf(makefile, "OBJS = splitvt.o misc.o utmp.o vt100.o videomem.o terminal.o vttest.o vtmouse.o \\\n");
  231.     fprintf(makefile, "       parserc.o lock.o cut-paste.o\n\n");
  232.     fprintf(makefile, "splitvt: $(OBJS)\n");
  233. #if defined(linux) && !defined(DEBUG)
  234.     fprintf(makefile, "\t$(CC) -s -o $@ $(OBJS) $(LIBS)\n");
  235. #else
  236.     fprintf(makefile, "\t$(CC) -o $@ $(OBJS) $(LIBS)\n");
  237. #endif
  238.     fprintf(makefile, "\nclean: \n\trm -f *.o core \n");
  239.     fprintf(makefile, "\nclobber: clean\n\trm -f splitvt Makefile\n");
  240.  
  241.     fclose(makefile);
  242.     exit(0);
  243. }
  244.  
  245.  
  246. /* Yeesh.  I have to write a word grep function.... */
  247.  
  248. int grep(dir, file, word)
  249. char *dir;
  250. char *file;
  251. char *word;
  252. {
  253.     FILE *fp;
  254.     char *wptr, *ptr, buffer[BUFSIZ];
  255.  
  256.     sprintf(buffer, "%s/%s", dir, file);
  257.     if ( (fp=fopen(buffer, "r")) == NULL )
  258.         return(0);
  259.     ptr=word;
  260.  
  261.     while ( fgets(buffer, BUFSIZ-1, fp) != NULL ) {
  262.         for ( wptr=buffer; *wptr; ++wptr) {
  263.             if ( *wptr == *ptr ) {
  264.                 ++ptr;
  265.                 if ( *ptr == '\0' ) {
  266.                     (void) fclose(fp);
  267.                     return(1);
  268.                 }
  269.             }
  270.             else
  271.                 ptr=word;
  272.         }
  273.     }
  274.     (void) fclose(fp);
  275.     return(0);
  276. }
  277.  
  278.